package day100.LC59;

public class Main {
    public static void main(String[] args) {

    }
}


class Solution {
    public int[][] generateMatrix(int n) {
        int[][] arr = new int[n][n];
        int i=0;//控制行
        int j=0;//控制列
        int offset=0;//偏移量
        int circulateNum=1;
        int num=1;
        while (circulateNum<=n/2) {
            for (; j < n-1-offset; j++) {
                arr[i][j]=num;
                num++;
            }
            for (; i < n-1-offset; i++) {
                arr[i][j]=num;
                num++;
            }
            for (;j>offset ;j-- ) {
                arr[i][j]=num;
                num++;
            }
            for (;i>offset ; i--) {
                arr[i][j]=num;
                num++;
            }
            i++;//回溯
            j++;//调整列
            offset++;
            circulateNum++;
        }
        if (n%2==1) {
            i=j=n/2;
            arr[i][j]=num;
        }
        return arr;
    }
}
